package monkeytest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import source.Common;

public class Logcat extends Thread {
	private boolean LogKey = false;
	public static void main(String[] args) {
		Logcat logcat = new Logcat();
		logcat.run();
	}

	@Override
	public void run() {
		execCmdAdb("adb logcat -c");
		Common.getInstance().sleep(1000);
		execCmdAdb("adb logcat", "logcat" + getNow() + ".log");
	}
	
	public void outputKey() {
		Common.getInstance().output(LogKey);
	}
	
	/**
	 * 执行adb logcat命令此方法已过滤掉I级别日常不做记录
	 * 
	 * @param cmd
	 *            命令内容
	 * @param fileName
	 *            输入文件路径
	 */
	private void execCmdAdb(String cmd, String fileName) {
		System.out.println(cmd);
		String OSname = System.getProperty("os.name");
		try {
			Process p = null;
			if (OSname.contains("Mac")) {
				p = Runtime.getRuntime().exec(Common.ADB_PATH + cmd);
			} else {
				p = Runtime.getRuntime().exec("cmd /c " + cmd);
			}
			// 正确输出流
			InputStream input = p.getInputStream();// 创建并实例化输入字节流
			BufferedReader reader = new BufferedReader(new InputStreamReader(input));// 先通过inputstreamreader进行流转化，在实例化bufferedreader，接收内容
			String line = "";
			while ((line = reader.readLine()) != null) {// 循环读取
				if (LogKey) {
					p.destroy();// 结束线程
					reader.close();// 此处reader依赖于input，应先关闭
					input.close();
					return;
				}
				//收集启动时间
				LauchTime.getActivityTimes(line);
				if (line.startsWith("I/") || line.contains(" I ")) {
					continue;
				}
				if (line.contains("happyjuzi")) {
					Common.getInstance().saveToFile(line, fileName);// 保存
				}
			}
			reader.close();// 此处reader依赖于input，应先关闭
			input.close();
			// 错误输出流
			InputStream errorInput = p.getErrorStream();// 创建并实例化输入字节流
			BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));// 先通过inputstreamreader进行流转化，在实例化bufferedreader，接收内容
			String eline = "";
			while ((eline = errorReader.readLine()) != null) {// 循环读取
				if (LogKey) {
					p.destroy();
					errorReader.close();// 此处有依赖关系，先关闭errorReader
					errorInput.close();
					return;
				}
				if (eline.contains("happyjuzi")) {
					Common.getInstance().saveToFile(line, fileName);// 保存
				}
			}
			errorReader.close();// 此处有依赖关系，先关闭errorReader
			errorInput.close();
		} catch (IOException e) {
			Common.getInstance().output("执行" + cmd + "失败！");
			e.printStackTrace();
		}
	}

	private void execCmdAdb(String cmd) {
		System.out.println(cmd);
		String OSname = System.getProperty("os.name");
		try {
			if (OSname.contains("Mac")) {
				Runtime.getRuntime().exec(Common.ADB_PATH + cmd);
			} else {
				Runtime.getRuntime().exec("cmd /c " + cmd);
			}
		} catch (IOException e) {
			Common.getInstance().output("执行" + cmd + "失败！");
			e.printStackTrace();
		}
	}

	/**
	 * 停止logcat线程
	 */
	public void stopLoacat() {
		this.LogKey = true;
	}

	/**
	 * 获取当前时间
	 * 
	 * @return 返回当前时间，只有日期和小时和分数没有年份和秒数
	 */
	private String getNow() {
		Date time = new Date();
		SimpleDateFormat now = new SimpleDateFormat("MM-dd-HH-mm");
		String c = now.format(time);
		return c;
	}
}
